<?php

namespace Boosen\Company\app\Console\Commands;

use Illuminate\Console\Command;

use App\Models\Region;
use Log;

use Goutte\Client;
use GuzzleHttp\Client as GuzzleClient;

class ProxySpider extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'proxy:get';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $client = new Client();
        $guzzleClient = new GuzzleClient(array(
            'timeout' => 30,
        ));
        $client->setClient($guzzleClient);
        $url = 'http://www.xicidaili.com/wn/';
        $crawler = $client->request('GET', $url);

        $items = $crawler->filterXPath('//table[@id="ip_list"]/tbody/tr');

        $ips = $items->each(function($node, $i) {
            return trim($node->filter('td')->eq(1)->text()) . ':' . trim($node->filter('td')->eq(2));
        });


        // $proxies = [];
        // $ips->each(function($node, $i) use (&$proxies, $ports) {
        //     $proxies[] = trim($node->text()) . ':' . trim($ports->eq($i)->text());
        // });
        // $key = array_rand($proxies);
        // $proxy = $proxies[$key];
        // print_r($proxy);exit;
        $crawler = $client->request('GET', "http://www.mdpi.cn/", array(
            'proxy' => 'http://114.234.82.138:9000'
        ));

        print_r($crawler->html());

    }
}
